package com.cn.bulu.utils.jiguang;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.cn.bulu.utils.Constant;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import cn.jpush.api.push.PushResult;

//发送推送
public class Jiguang {

	public static void main(String[] args) {
		Map<String, Object> map = new HashMap<>();
		map.put("address", "测试数据测试数据测试数据");
		map.put("subscribeTime", "2017-3-21 14:59:03");
		map.put("province", "宁夏回族自治区");
		map.put("city", "白银市");
		map.put("area", "会宁县");
		map.put("remark", "测试数据测试数据测试数据测试数据");
		map.put("addPrice", 55.0);
		map.put("weiXbody", "无");
		map.put("quCity", "");
		map.put("type", 0);

		map.put("phone", "15835316453");
		map.put("beginTime", "1489496820000");
		map.put("indentId", 88888);
		map.put("indentPrice", "80.0");
		map.put("name", "你了");
		map.put("quAddress", "");
		map.put("roomNumB", "6");
		map.put("status", " 0");
		map.put("cuAdId", 200);
		map.put("merchantsPrice", 90.0);
		map.put("number", 5);
		map.put("areaCode", "730901");
		JpushIndent(map);

	}

	public static void start(String ALERT, String MSG_CONTENT, Integer type, Map<String, String> extras, String city) {
		JPushClient jpushClient = null;
		if (type == 0) {// 新订单
			jpushClient = new JPushClient(Constant.MASTER_SECRET, Constant.APP_KEY, null, ClientConfig.getInstance());
		}
		// For push, all you need do is to build PushPayload object.
		PushPayload payload = buildPushObject_ios_tagAnd_alertWithExtrasAndMessage(ALERT, MSG_CONTENT, extras, city);

		try {
			PushResult result = jpushClient.sendPush(payload);
			// LOG.info("Got result - " + result);

		} catch (APIConnectionException e) {
			// Connection error, should retry later
			// LOG.error("Connection error, should retry later", e);
			e.printStackTrace();

		} catch (APIRequestException e) {
			// Should review the error, and fix the request
			// LOG.error("Should review the error, and fix the request", e);
			// LOG.info("HTTP Status: " + e.getStatus());
			// LOG.info("Error Code: " + e.getErrorCode());
			// LOG.info("Error Message: " + e.getErrorMessage());
			e.printStackTrace();
		}
	}

	public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage(String ALERT, String MSG_CONTENT,
			Map<String, String> extras, String city) {
		return PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.tag(city))
				.setNotification(Notification.newBuilder()
						.addPlatformNotification(IosNotification.newBuilder().setAlert(ALERT).setBadge(1).autoBadge()
								.setSound("default").setContentAvailable(false).setMutableContent(true).addExtra("from", "JPush").build())
						.build())
				.setMessage(Message.newBuilder().setMsgContent(MSG_CONTENT).addExtras(extras).build())
				.setOptions(Options.newBuilder().setApnsProduction(false).setTimeToLive(0).build()).build();
	}

	// 推送新订单或者加价悬赏
	public static void JpushIndent(Map<String, Object> map) {
		Map<String, String> send = new HashMap<String, String>();
		for (Entry<String, Object> entry : map.entrySet()) {
			send.put(entry.getKey(), entry.getValue() == null ? "".trim() : entry.getValue().toString());
		}
		// 进行推送 0推送订单 1推送消息
		send.put("msgType", "0");
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		try {
			System.out.println("sutime:"+map.get("subscribeTime").toString());
			date = simpleDateFormat.parse(map.get("subscribeTime").toString());
			System.out.println("after date:"+date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long timeStemp = date.getTime();
		send.put("subscribeTime", String.valueOf(timeStemp));
		Integer type = send.get("type") == null || "".equals(send.get("type")) ? null
				: Integer.valueOf(send.get("type").toString());
		Double addPrice = send.get("addPrice") == null || "".equals(send.get("addPrice")) ? 0.0
				: Double.valueOf(send.get("addPrice").toString());
		Double indentPrice = send.get("indentPrice") == null || "".equals(send.get("indentPrice")) ? 0.0
				: Double.valueOf(send.get("indentPrice").toString());
		String areaCode = send.get("areaCode") == null || "".equals(send.get("areaCode")) ? null
				: send.get("areaCode").toString();
		Integer motorNum = send.get("motorNum") == null || "".equals(send.get("motorNum")) ? 0
				: Integer.valueOf(send.get("motorNum"));
		System.out.println("type:" + type + "/addPrice:" + addPrice + "/indentPrice:" + indentPrice
				+ "/areaCode:" + areaCode + "/motorNum:" + motorNum);
		if (type == null || areaCode == null)
			return;
		if (type == 0) {// 这是测量订单
			// 判断一下是否加价
			if (addPrice != 0.0) {// 加价了
				Jiguang.start("测量订单￥" + indentPrice + "加价￥" + addPrice,
						"测量订单￥" + indentPrice + "加价￥" + addPrice, 0, send, areaCode);// 按区域码发送推送
			} else {// 未加价
				Jiguang.start("测量订单￥" + indentPrice, "测量订单￥" + indentPrice, 0, send, areaCode);
			}
		} else if (type == 1) {// 这是安装订单
			if (addPrice != 0.0) {// 这是加价了
				if (motorNum != 0) {// 含电动
					Jiguang.start("安装订单含电动窗帘￥" + indentPrice + "加价￥" + addPrice,
							"安装订单含电动窗帘￥" + indentPrice + "加价￥" + addPrice, 0, send, areaCode);
				} else {// 不含电动
					Jiguang.start("安装订单￥" + indentPrice + "加价￥" + addPrice,
							"安装订单￥" + indentPrice + "加价￥" + addPrice, 0, send, areaCode);
				}
			} else {// 这是没有加价
				if (motorNum != 0) {// 含电动
					Jiguang.start("安装订单含电动窗帘￥" + indentPrice, "安装订单含电动窗帘￥" + indentPrice, 0, send,
							areaCode);
				} else {// 不含电动
					Jiguang.start("安装订单￥" + indentPrice, "安装订单￥" + indentPrice, 0, send, areaCode);
				}
			}
		} else {// 这是维修订单
			if (addPrice != 0.0) {// 这是加价了
				if (motorNum != 0) {// 含电动
					Jiguang.start("维修订单含电动窗帘￥" + indentPrice + "加价￥" + addPrice,
							"维修订单含电动窗帘￥" + indentPrice + "加价￥" + addPrice, 0, send, areaCode);
				} else {
					Jiguang.start("维修订单￥" + indentPrice + "加价￥" + addPrice,
							"维修订单￥" + indentPrice + "加价￥" + addPrice, 0, send, areaCode);
				}
			} else {// 这是没有加价
				if (motorNum != 0) {// 含电动
					Jiguang.start("维修订单含电动窗帘￥" + indentPrice, "维修订单含电动窗帘￥" + indentPrice, 0, send,
							areaCode);
				} else {// 不含电动
					Jiguang.start("维修订单￥" + indentPrice, "维修订单￥" + indentPrice, 0, send, areaCode);
				}
			}
		}
	}
}
